var tgd = tgd || {};
tgd.Utilities = tgd.Utilities || {};
tgd.Utilities.Countdown = tgd.Utilities.Countdown || {};

/*
 * "Overloaded" constructor
 * viewsOrQueryString = array of tgd.Utilities.Countdown.Views.HTMLTimer objects or window.location.search
 */
tgd.Utilities.Countdown.Serializer = function (viewsOrQueryString) {
	this._timers = null;
	this._queryString = null;

	if (viewsOrQueryString instanceof Array) {
		this._timers = new Array(viewsOrQueryString.length);
		this._queryString = "";
		for (var i = 0; i < this._timers.length; ++i) {
			this._timers[i] = viewsOrQueryString[i].getTimer();
			if (!viewsOrQueryString[i].isClosed()) {
				this._queryString += "t" + i + "=" + this._timers[i].getEndDateTime() + "," + this._timers[i].getTitle() + "&";
			}
		}
	} else {
		this._queryString = viewsOrQueryString;
		this._timers = new Array();

		if (this._queryString != "") {
			var timerStringArray = this._queryString.substr(1).split("&");
			var equalsIndex;

			for (var i = 0; i < timerStringArray.length; ++i) {
				if ((equalsIndex = timerStringArray[i].indexOf("=")) > 0) {
					var timerOpts = timerStringArray[i].substr(equalsIndex + 1).split(",");
					this._timers[this._timers.length] = new tgd.Utilities.Countdown.Timer({Title: timerOpts[1], EndDateTime: timerOpts[0]});
				}
			}
		}
	}

	if (tgd.Utilities.Countdown.Serializer._initialized === undefined) {

		tgd.Utilities.Countdown.Serializer.prototype.getSerialization = function() {
			return this._queryString;
		}

		tgd.Utilities.Countdown.Serializer.prototype.getTimers = function() {
			return this._timers;
		}

		tgd.Utilities.Countdown.Serializer._initialized = true;
	}
};